extension ListEx<E> on List<E> {
  /// Returns the first element safely.
  /// If the list is empty, returns `null`.
  E? get safeFirst => isNotEmpty ? this[0] : null;

  /// Returns the last element safely.
  /// If the list is empty, returns `null`.
  E? get safeLast => isNotEmpty ? this[length - 1] : null;

  /// Returns the element at the specified index safely.
  /// Returns `null` if the index is out of bounds.
  E? safeAt(int index) => (index >= 0 && index < length) ? this[index] : null;
}